package com.cseg674.dataentry;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.cseg674.dataentry.javaparser.DEApplication;
import com.cseg674.datamanagement.adler.Adler;
import com.cseg674.datamanagement.adler.AdlerException;

public class DataEntryServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		FileItem fileItem = null;
		ArrayList<FileItem> files = new ArrayList<FileItem>();
		String projectName = "";
		try {
			boolean isMultipart = ServletFileUpload.isMultipartContent(request);

			if (isMultipart) {
				FileItemFactory factory = new DiskFileItemFactory();
				ServletFileUpload upload = new ServletFileUpload(factory);
				List items = upload.parseRequest(request);
				Iterator iter = items.iterator();
				
				while (iter.hasNext()) {
					FileItem item = (FileItem) iter.next();

					if (item.isFormField()) {
						String fieldname = item.getFieldName();
						String fieldvalue = item.getString();
						if (fieldname.equals("projectname")) {
							projectName = fieldvalue;
						}
					}

					if (!item.isFormField()) {
						fileItem = item;
						files.add(fileItem);
					}
				}
			}else{
				// non multipart post //
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
			
		try {
			Adler adler = new Adler();
	
			BufferedReader br; 
			String semanticData = ""; 
			String line;

			try{ 
				br = new BufferedReader(new FileReader(DEApplication.parseFiles(files))); 
				
				while((line = br.readLine()) != null) { 
					semanticData += line;  
				} 				
				adler.insertProject(projectName, semanticData, "", "RDF/XML");
				
				request.getSession().setAttribute("msg", "Your project has been uploaded successfully!");
				RequestDispatcher rd = request.getRequestDispatcher("query.jsp");
				rd.forward(request, response);
			} catch(Exception e) { 
				e.printStackTrace();
				request.getSession().setAttribute("msg", "Error: Unable to upload project! Please try again.");
				RequestDispatcher rd = request.getRequestDispatcher("query.jsp");
				rd.forward(request, response);
			}  
			
		} catch (AdlerException e) {
			// handle AdlerException
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)	throws IOException, ServletException {
		// no doGet //
	}

}

